<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Log;
use App\User;
use App\user_datail;
use Hash;
use Illuminate\Http\Request;

class PeopleController extends Controller {

	//显示个人主页
	public function index(Request $request) {
		// $class = ['users', 'articles', 'questions', 'topics', 'columns'];
		$active = ['收藏了', '关注了', '评论了', '添加了', '删除了'];
		$user = User::find(session('id'));
		// dd($user->datail);
		$datail = $user->datail;
		$discuss = $user->discuss;
		$question = $user->question;
		$article = $user->article;
		$collection = $user->collection;
		$logs = Log::where('user_id', session('id'))->orderBy('id', 'desc')->paginate(10);
		foreach ($logs as $k => $v) {
			$logs[$k]->active = $active[$v->active - 1];
		}
		return view('home.people', ['user' => $user, 'logs' => $logs]);
	}

	// 个人设置
	public function setting() {
		$user = User::find(session('id'));
		// dd($user->datail);
		return view('home.setting', ['user' => $user]);
		// dd($user);
	}

	// 密码重置
	public function repasswd() {
		$user = User::find(session('id'));
		return view('home.passwd', ['user' => $user]);
	}

	// 访问他人主页
	public function show($id) {
		if ($id == session('id')) {
			$self = true;
		} else {
			$self = false;
		}
		$class = ['users', 'articles', 'questions', 'topics', 'columns'];
		$active = ['收藏了', '关注了', '评论了', '添加了', '删除了'];
		$user = User::find($id);
		$datail = $user->datail;
		$discuss = $user->discuss;
		$question = $user->question;
		$article = $user->article;
		$collection = $user->collection;
		$logs = Log::where('user_id', $id)->orderBy('id', 'desc')->paginate(10);
		foreach ($logs as $k => $v) {
			$logs[$k]->active = $active[$v->active - 1];
		}
		return view('home.people', ['user' => $user, 'logs' => $logs, 'self' => $self]);
	}
	//显示登录注册页面
	public function login(Request $request) {
		return view('home.login');
	}
	//处理登录
	public function doLogin(Request $request) {
		$this->validate($request, [
			'username' => 'required|regex:/^\w{5,18}$/',
			'passwd' => 'required',
		], [
			'username.required' => '用户名不能为空',
			'username.regex' => '用户名格式正确',
			'passwd.required' => '密码不能为空',
		]);
		$user = User::where('username', $request->username)->first();
		if (!$user) {
			return back()->with('error', '用户名或密码错误');
		}
		session([
			'id' => $user->id,
			'name' => $user->name,
			'username' => $user->username,
			'face' => $user->face]);
		return redirect('/');
	}
	//注销登录
	public function logout(Request $request) {
		$request->session()->forget('id');
		$request->session()->forget('username');
		$request->session()->forget('name');
		$request->session()->forget('face');
		return redirect('/login');
	}
	//处理注册
	public function register(Request $request) {
		//创建用户模型
		$user = new User;
		$name = str_random(5);
		//设置属性值
		$user->username = $request->username;
		$user->name = $name;
		$user->password = Hash::make($request->password);
		$user->phone = $request->phone;
		//保存数据
		if ($user->save()) {
			$datail = new user_datail;
			$datail->user_id = $user->id;
			$datail->save();
			//注册成功, 写入session, 自动登录
			session(['id' => $user->id, 'name' => $user->name, 'username' => $user->username, 'face' => '/uploads/temp.jpg']);
			return redirect('/');
		} else {
			//注册失败, 返回注册页面
			return back()->with('error', '注册失败 ! 请稍后重试 !');
		}
	}
	//检测用户名是否存在
	public function checkUname(Request $request) {
		$user = User::where('username', $request->username)->first();
		if ($user) {
			return 0;
		} else {
			return 1;
		}
	}
}
